<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>请假申请</title>
    <link rel="stylesheet" href="./index.css" />
    <style>
      #leaveRequest {
        height: 100vh;
        background-color: #f5f5f5;
      }
    </style>
  </head>

  <body>
    <div id="leaveRequest">
      <div>
        <van-nav-bar
          title="请假申请"
          left-text="返回"
          left-arrow
          @click-left="onClickLeft"
        />
      </div>
      <div style="padding: 10px">
        <div style="background-color: #fff; padding: 0 px; border-radius: 5px">
          <div>
            <van-cell is-link :value="leaveType" @click="showPopup('1')">
              <template #title>
                <span class="custom-title">请假类型</span>
                <span style="color: red">*</span>
              </template>
            </van-cell>
          </div>
          <div
            style="height: 2px; background-color: #f5f5f5; margin: 0 20px"
          ></div>
          <div>
            <van-cell is-link :value="defaultStart" @click="showPopup('2')">
              <template #title>
                <span class="custom-title">开始时间</span>
                <span style="color: red">*</span>
              </template>
            </van-cell>
          </div>

          <div>
            <van-cell is-link :value="defaultEnd" @click="showPopup('3')">
              <template #title>
                <span class="custom-title">结束时间</span>
                <span style="color: red">*</span>
              </template>
            </van-cell>
          </div>
        </div>
      </div>
      <div style="padding: 10px">
        <div style="background-color: #fff; padding: 0 px; border-radius: 5px">
          <van-field
            v-model="duration1"
            center
            clearable
            label="时长"
            placeholder="请输入时长"
            :disabled="isId"
          >
            <!-- <template #button> 小时 </template> -->
          </van-field>
        </div>
      </div>
      <div style="padding: 10px">
        <div style="background-color: #fff; padding: 0 px; border-radius: 5px">
          <van-field
            v-model="duration2"
            center
            clearable
            label="时长"
            placeholder="请输入时长"
            :disabled="!isId"
          >
            <template #button> 天 </template>
          </van-field>
        </div>
      </div>

      <div style="padding: 10px">
        <div style="background-color: #fff; padding: 0 px; border-radius: 5px">
          <div>
            <van-cell title="说明附件">
              <template #right-icon>
                <van-uploader>
                  <van-icon name="plus" />
                </van-uploader>
              </template>
            </van-cell>
          </div>
        </div>
      </div>
      <div style="padding: 10px">
        <div style="background-color: #fff; padding: 20px; border-radius: 5px">
          <div style="font-size: 14px; color: #323233">
            请假事由<span style="color: red">*</span>
          </div>
          <van-field
            v-model="textValue"
            rows="3"
            autosize
            type="textarea"
            placeholder="请输入请假事由"
          />
        </div>
      </div>
      <div style="padding: 10px" v-if="isFormValid">
        <div
          style="
            background-color: #fff;
            padding: 0 0 0 10px;
            border-radius: 5px;
          "
        >
          <div style="display: flex">
            <div style="display: flex; flex-direction: column">
              <div
                style="
                  width: 32px;
                  height: 32px;
                  border-radius: 50%;
                  background-color: red;
                  margin-top: 10px;
                "
              ></div>
              <div
                style="
                  width: 2px;
                  height: 58px;
                  background-color: royalblue;
                  margin-left: 16px;
                "
              ></div>
              <div
                style="
                  width: 32px;
                  height: 32px;
                  border-radius: 50%;
                  background-color: forestgreen;
                "
              ></div>
            </div>
            <div>
              <div style="padding: 10px; width: 100%">
                <div>吴**（申请人）</div>
                <div
                  style="
                    display: flex;
                    justify-content: space-between;
                    line-height: 20px;
                    margin-top: 10px;
                  "
                >
                  <div>销售A组组长</div>
                  <div>2025.07.10 15:30</div>
                </div>
              </div>
              <div style="padding: 10px; width: 100%; margin-top: 24px">
                <div>吴**（申请人）</div>
                <div
                  style="
                    display: flex;
                    justify-content: space-between;
                    line-height: 20px;
                    margin-top: 10px;
                  "
                >
                  <div>销售A组组长</div>
                  <div>2025.07.10 15:30</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div
        style="
          position: fixed;
          bottom: 0;
          left: 0;
          right: 0;
          padding: 15px;
          background-color: white;
          box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
          z-index: 1000;
        "
      >
        <van-button type="danger" block @click="onSubmit"> 提交</van-button>
      </div>
      <van-popup position="bottom" v-model="show">
        <div v-if="popupType === '1'">
          <van-picker
            title="请选择请假类型"
            show-toolbar
            :columns="columns"
            @confirm="onConfirm"
            @cancel="show = false"
            @change="onChange"
            value-key="text"
          />
        </div>
        <div v-else-if="popupType === '2'">
          <van-datetime-picker
            v-model="startTime"
            type="datetime"
            title="选择完整时间"
            :min-date="minDate"
            :max-date="maxDate"
            @confirm="onPickerConfirm"
            @cancel="show = false"
          />
        </div>
        <div v-else-if="popupType === '3'">
          <van-datetime-picker
            v-model="endTime"
            type="datetime"
            title="选择完整时间"
            :min-date="minDate"
            :max-date="maxDate"
            @confirm="onPickerConfirm"
            @cancel="show = false"
          />
        </div>
      </van-popup>
    </div>
  </body>
  <script src="./vue.min.js"></script>
  <script src="./vant.min.js"></script>
  <script>
    var leaveRequest = new Vue({
      el: "#leaveRequest",
      data() {
        const now = new Date();
        const minDate = new Date(now);
        minDate.setFullYear(now.getFullYear() - 1);
        const maxDate = new Date(now);
        maxDate.setFullYear(now.getFullYear() + 1);
        return {
          popupType: "", // 弹出类型
          minDate, // 去年今日
          maxDate, // 明年今日
          selectedType: "",
          startTime: new Date(), // 组件默认时间设置今天
          endTime: new Date(), // 组件默认时间设置今天
          leaveType: "请选择", //  请假类型名
          leaveTypeId: "", //  请假类型id
          defaultStart: "请选择", //  请假开始时间
          defaultEnd: "请选择", //  请假结束时间
          textValue: "", //  请假理由
          duration1: "", //  请假时长
          duration2: "", //  请假时长
          columns: [
            { id: 322, text: "年假" },
            { id: 324, text: "病假" },
            { id: 325, text: "婚假" },
            { id: 885, text: "侍产假" },
            { id: 886, text: "丧假" },
            { id: 890, text: "产假" },
            {
              id: 888,
              text: " 无薪假",
            },
            { id: 889, text: "其它(只适用于新加坡公司)" },
            { id: 323, text: "事假" },
            { id: 887, text: "补休" },
          ],
          // columns: ["调休", "病假", "事假", "年假"],
          show: false,
        };
      },
      computed: {
        isFormValid() {
          return (
            this.leaveTypeId && // 不是 falsy（比如空字符串 / null / undefined）
            this.defaultStart !== "请选择" && // 不是默认占位文本
            this.defaultEnd !== "请选择" && // 不是默认占位文本
            this.textValue.trim() !== "" // 不是空字符串
          );
        },
        isId() {
          return this.leaveTypeId === 323 || this.leaveTypeId === 887;
        },
      },
      created() {},
      mounted() {},
      methods: {
        onClickLeft() {
          console.log("点击了返回");
          window.history.back();
        },
        showPopup(value) {
          //  点击打开弹出层
          this.popupType = value;
          this.show = true;
        },
        onPickerConfirm() {
          //  点击确认选择日期
          if (this.popupType === "1") {
            this.defaultStart = this.selectedType;
          } else if (this.popupType === "2") {
            this.defaultStart = this.formatDate(this.startTime);
            this.startTime = new Date(this.defaultStart);
          } else if (this.popupType === "3") {
            this.defaultEnd = this.formatDate(this.endTime);
            this.endTime = new Date(this.defaultEnd);
          }
          // if (this.defaultStart !== "请选择" && this.defaultEnd !== "请选择") {
          //   console.log("都有值");
          //   this.onTimeDifference();
          // } else {
          //   // alert("请选择请假类型");
          //   // return;
          // }
          this.show = false;
        },
        formatDate(date) {
          // 格式化日期为 YYYY-MM-DD HH:mm 格式
          const year = date.getFullYear();
          const month = String(date.getMonth() + 1).padStart(2, "0");
          const day = String(date.getDate()).padStart(2, "0");
          const hours = String(date.getHours()).padStart(2, "0");
          const minutes = String(date.getMinutes()).padStart(2, "0");
          return `${year}-${month}-${day} ${hours}:${minutes}`;
        },
        // onTimeDifference() {
        //   // 计算时间差
        //   const timeDifference = this.endTime - this.startTime;
        //   const hours = Number((timeDifference / (1000 * 60 * 60)).toFixed(2));
        //   this.duration = hours;
        //   console.log(hours, "时间差");
        // },
        onConfirm(value, index) {
          // 点击确认选择项目
          // console.log(value, "对象内容", index, "索引");
          this.leaveType = value.text;
          this.leaveTypeId = value.id;
          console.log(this.leaveType, this.leaveTypeId);
          this.show = false;
        },
        onChange(picker, value, index) {
          // 选择项目时触发
          // console.log(picker, "实例", value, "对象内容", index, "索引");
          // alert("提示内容");
        },
        onSubmit() {
          if (!this.leaveTypeId) {
            this.$toast("请选择请假类型");
            return;
          }
          if (this.defaultStart === "请选择") {
            this.$toast("请选择开始时间");
            return;
          }
          if (this.defaultEnd === "请选择") {
            this.$toast("请选择结束时间");
            return;
          }
          if (this.endTime < this.startTime) {
            this.$toast("结束时间不能早于开始时间");
            return;
          }
          if (!this.textValue) {
            this.$toast("请输入请假理由");
            return;
          }
          // 提交表单
          console.log(
            this.leaveType,
            this.leaveTypeId,
            this.defaultStart,
            this.defaultEnd,
            this.duration1,
            this.duration2,
            this.textValue,
            this.isId
          );
        },
      },
    });
  </script>
</html>
